import {TaskBase} from "./TaskBase";
type conditiion = (conditiion:ConditionalTask)=>boolean;
type condAction = (conditiion:ConditionalTask,args:string)=>void;

export class ConditionalTask extends TaskBase {
    private _condition:conditiion;
    private _completeAction:condAction;
    private _failureAction:condAction;
    private _cancelAction:condAction;

    constructor() {
        super();
        this._condition = null;
        this._completeAction = null;
        this._failureAction = null;
        this._cancelAction = null;
    }

    SetCondition(cond:conditiion):void{
        this._condition = cond;
    }

    SetCompleteAction(completeAction:condAction):void{
        this._completeAction = completeAction;
    }

    SetFailureAction(failureAction:condAction):void{
        this._failureAction = failureAction;
    }

    SetCancelAction(cancelAction:condAction):void{
        this._cancelAction = cancelAction;
    }


     OnStart(): void {
        super.OnStart();
        if (this._condition == null)
        {
            this.OnFailure("Condition is invalid.");
            return;
        }
        if (this._completeAction == null)
        {
            this.OnFailure("Complete action is invalid.");
            return;
        }

    }

     OnUpdate(elapseSeconds: number, realElapseSeconds: number): void {
        super.OnUpdate(elapseSeconds, realElapseSeconds);
        if (!this._condition(this))
        {
            return;
        }

        this.OnComplete("Conditional reach");
    }

     OnComplete(reason: string): void {
        super.OnComplete(reason);
        this._completeAction && this._completeAction(this,reason);
    }

     OnFailure(reason: string): void {
        super.OnFailure(reason);
        this._failureAction && this._failureAction(this,reason);
    }

     OnCancel(reason: string): void {
        super.OnCancel(reason);
        this._cancelAction && this._cancelAction(this,reason);
    }

}

